package com.gd.iot.energy.service.datamanage.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.datamanage.EquipInstructionValuePOMapper;
import com.gd.iot.energy.dao.datamanage.EquipInstructiontreePOMapper;
import com.gd.iot.energy.po.datamanage.EquipInstructionValuePO;
import com.gd.iot.energy.po.datamanage.EquipInstructionValuePOWithBLOBs;
import com.gd.iot.energy.po.datamanage.EquipInstructiontreePO;
import com.gd.iot.energy.pojo.common.vo.ColumnVO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.statistics.vo.AllDataCallPageVO;
import com.gd.iot.energy.service.datamanage.AllDataCallService;
import com.gd.iot.energy.service.systemadmin.AreaOrganizationService;
import com.gd.iot.energy.util.CustomColumnUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kris
 * @date 2020/8/14 0014 8:42
 */
@Service
public class AllDataCallServiceImpl implements AllDataCallService {

    @Autowired
    private AreaOrganizationService areaOrganizationService;

    @Resource
    private EquipInstructionValuePOMapper equipInstructionValuePOMapper;

    @Resource
    private EquipInstructiontreePOMapper equipInstructiontreePOMapper;

    /**
     * 数据召测记录表头配置文件
     */
    private static final String ALL_DATA_CALL_LIST_COLUMN_FILE_NAME = "datamanage/all_data_call_list.column";


    @Override
    public PageVO<AllDataCallPageVO> allDataCall(Integer areaId, Integer pageSize, Integer pageNum) {
        IPage<EquipInstructionValuePOWithBLOBs> page = new Page<>(pageNum, pageSize);
        QueryWrapper<EquipInstructionValuePOWithBLOBs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        if (areaId != null) {
            List<Long> allChildAreaIds = areaOrganizationService.getAllChildAreaIds(areaId);
            if (CollUtil.isNotEmpty(allChildAreaIds)) {
                queryWrapper.in("area_org_id", allChildAreaIds);
            }
        }
        queryWrapper.orderByDesc("create_date");
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        IPage<EquipInstructionValuePOWithBLOBs> equipInstructionValuePOPage = equipInstructionValuePOMapper.selectPage(page, queryWrapper);
        PageVO<AllDataCallPageVO> pageVO = new PageVO<>();
        pageVO.setPageSize(pageSize);
        pageVO.setPageNum(pageNum);
        List<AllDataCallPageVO> roleVOList = convertToAllDataCallPageVOList(equipInstructionValuePOPage.getRecords());
        pageVO.setData(roleVOList);
        pageVO.setTotal(equipInstructionValuePOPage.getTotal());
        pageVO.setPages(equipInstructionValuePOPage.getPages());

        //表头
        List<ColumnVO> columns = CustomColumnUtil.getColumn(ALL_DATA_CALL_LIST_COLUMN_FILE_NAME);
        pageVO.setColumns(columns);
        return pageVO;
    }

    /**
     * 数据召测po转vo
     * @param records
     * @return
     */
    private List<AllDataCallPageVO> convertToAllDataCallPageVOList(List<EquipInstructionValuePOWithBLOBs> records) {
        List<Long> treeIds = records.stream().map(EquipInstructionValuePO::getTreeId).collect(Collectors.toList());
        List<EquipInstructiontreePO> equipInstructiontreePOS = equipInstructiontreePOMapper.selectBatchIds(treeIds);
        Map<Long, String> treeIdNameMap = equipInstructiontreePOS.stream().collect(Collectors.toMap(EquipInstructiontreePO::getId, EquipInstructiontreePO::getTreeName));
        List<AllDataCallPageVO> res = new ArrayList<>(records.size());
        AllDataCallPageVO allDataCallPageVO;
        long i = 1;
        for (EquipInstructionValuePOWithBLOBs record : records) {
            allDataCallPageVO = new AllDataCallPageVO();
            allDataCallPageVO.setId(record.getId());
            allDataCallPageVO.setSerialNum(i);
            allDataCallPageVO.setDoCount(record.getDoCount() == null ? null : record.getDoCount().toString());
            allDataCallPageVO.setPnReceiveTime(record.getPnReceiveTime());
            allDataCallPageVO.setSendDesc(treeIdNameMap.get(record.getTreeId()));
            allDataCallPageVO.setPnSendTime(record.getPnSendTime());
            allDataCallPageVO.setSend("指令详情");
            allDataCallPageVO.setReceive(StrUtil.isBlank(record.getPnReceiveTime())?"未完成":"完成");
            allDataCallPageVO.setUsername(record.getUsername());
            allDataCallPageVO.setProgress(SystemConsts.InstructionStateEnum.getProgressByValue(record.getState()));
            res.add(allDataCallPageVO);
            i++;
        }
        return res;
    }
}
